//
//  MJ_PlatDetailLayout.m
//  UEAssistant
//
//  Created by mac on 2018/2/9.
//  Copyright © 2018年 youyixinxi. All rights reserved.
//

#import "MJ_PlatDetailLayout.h"

@implementation MJ_PlatDetailLayout


- (id)copyWithZone:(NSZone *)zone {
    
    MJ_PlatDetailLayout *layout = [[MJ_PlatDetailLayout alloc] init];
    layout.cellHeight = self.cellHeight;
    return layout;
}

- (instancetype)initWithModel:(MJ_TaskListResult *)model type:(MJTaskOrder)type index:(NSInteger)index
{
    self = [super init];
    if (self) {
        if (index == 0) {
            //文件名字
            LWTextStorage *fileName = [[LWTextStorage alloc] init];
            fileName.frame = CGRectMake(18, 16, Screen_Width-36, CGFLOAT_MAX);
            fileName.font = [UIFont boldSystemFontOfSize:16];
            fileName.textColor = [UIColor colorWithHexString:@"#333333"];
            fileName.text = model.doc_name;
            [self addStorage:fileName];
            //平台待抢单
            if (type == MJTaskOrderWaitWithPlat) {//平台待抢单...
                NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:model.price attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#ff6f4c"]}];
                [str appendAttributedString:[[NSAttributedString alloc] initWithString:@"元/千字" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#999999"]}]];
                CGRect frame = CGRectMake(18, fileName.bottom+12, Screen_Width, CGFLOAT_MAX);
                LWTextStorage *priceLabel = [LWTextStorage lw_textStorageWithText:str frame:frame];
                [self addStorage:priceLabel];
                
                LWTextStorage *checkAllContent = [[LWTextStorage alloc] init];
                checkAllContent.text = @"查看全文";
                checkAllContent.frame = CGRectMake(Screen_Width- 90, fileName.bottom+12, 60, 24);
                checkAllContent.textColor = [UIColor colorWithHexString:@"#0B8235"];
                checkAllContent.font = [UIFont systemFontOfSize:14];
                [checkAllContent lw_addLinkWithData:@"checkAll"
                                              range:NSMakeRange(0, 4)
                                          linkColor:[UIColor colorWithHexString:@"#0B8235"]
                                     highLightColor:nil];
                [self addStorage:checkAllContent];
                
                LWImageStorage *moreIcon = [[LWImageStorage alloc] init];
                moreIcon.contents = [UIImage imageNamed:@"more"];
                moreIcon.frame = CGRectMake(checkAllContent.right+5, checkAllContent.top, 12, 12);
                [self addStorage:moreIcon];
                
            }else if (type == MJTaskOrderOngoingWithPlat) {//平台进行中...
                
                LWTextStorage *producedAmount = [[LWTextStorage alloc] init];
                producedAmount.text = @"已产生金额";
                producedAmount.frame = CGRectMake(18, fileName.bottom+12, 100, CGFLOAT_MAX);
                producedAmount.textColor = [UIColor colorWithHexString:@"#314659"];
                producedAmount.font = [UIFont systemFontOfSize:14];
                [self addStorage:producedAmount];
                
                LWTextStorage *producedWordCount = [[LWTextStorage alloc] init];
                producedWordCount.textColor = [UIColor colorWithHexString:@"#314659"];
                producedWordCount.font = [UIFont systemFontOfSize:14];
                producedWordCount.text = [NSString stringWithFormat:@"%@元/千字*999字",model.price];
                producedWordCount.textAlignment = NSTextAlignmentRight;
                producedWordCount.frame = CGRectMake(producedAmount.right+5,
                                                     producedAmount.top,
                                                     Screen_Width-producedAmount.width-41,
                                                     producedAmount.height);
                [self addStorage:producedWordCount];
                
                LWTextStorage *price = [[LWTextStorage alloc] init];
                price.text = @"￥122.8";
                price.textColor = [UIColor colorWithHexString:@"#ff6f4c"];
                price.font = [UIFont systemFontOfSize:18];
                price.textAlignment = NSTextAlignmentRight;
                price.frame = CGRectMake(18, producedWordCount.bottom+6, Screen_Width-36, CGFLOAT_MAX);
                [self addStorage:price];
                
            }else{//试译中...
                NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:model.price attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#ff6f4c"]}];
                [str appendAttributedString:[[NSAttributedString alloc] initWithString:@"元/千字" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#999999"]}]];
                CGRect frame = CGRectMake(18, fileName.bottom+12, Screen_Width, CGFLOAT_MAX);
                LWTextStorage *priceLabel = [LWTextStorage lw_textStorageWithText:str frame:frame];
                [self addStorage:priceLabel];
            }
            
        }else if (index == 1) {
            //下单人底部分割线
            LWImageStorage *bottomLineForBuyer = [[LWImageStorage alloc] init];
            bottomLineForBuyer.backgroundColor = [UIColor colorWithHexString:@"#e0e0e0"];
            bottomLineForBuyer.frame = CGRectMake(18, 0, 0, 0);
            if (type == MJTaskOrderOngoingWithPlat) {//
                //"下单人标题"
                LWTextStorage *buyerTitle = [self setupStorageWithFontSize:13 textColor:@"#666666"];
                buyerTitle.text = @"下单人";
                //下单人icon
                LWImageStorage *buyerIcon = [[LWImageStorage alloc] init];
                buyerIcon.contents = [UIImage imageNamed:@"下单人"];
                //下单人姓名
                LWTextStorage *buyerName = [self setupStorageWithFontSize:14 textColor:@"#333333"];
                buyerName.text = @"Distance";
                //下单人电话号码
                LWTextStorage *buyerPhoneNumber = [self setupStorageWithFontSize:14 textColor:@"#333333"];
                buyerPhoneNumber.text = @"18200551181";
                buyerPhoneNumber.textAlignment = NSTextAlignmentRight;
                //图标前的竖线
                LWImageStorage *phoneLine = [[LWImageStorage alloc] init];
                phoneLine.backgroundColor = [UIColor colorWithHexString:@"#e0e0e0"];
                //拨打电话图标
                LWImageStorage *phoneIcon = [[LWImageStorage alloc] init];
                phoneIcon.tag = 4096;
                phoneIcon.contents = [UIImage imageNamed:@"手机"];
 
                //设置布局
                buyerTitle.frame = CGRectMake(18, 12, 100, CGFLOAT_MAX);//下单人标题
                buyerIcon.frame = CGRectMake(buyerTitle.left, buyerTitle.bottom+12, 15, 15);//下单人icon
                buyerName.frame = CGRectMake(buyerIcon.right+8, buyerIcon.top, 100, 15);
                phoneIcon.frame = CGRectMake(Screen_Width-46, (buyerName.bottom+18)/2.0-11, 22, 22);
                bottomLineForBuyer.frame = CGRectMake(18, buyerName.bottom+18, Screen_Width-36, 0.5);
                phoneLine.frame = CGRectMake(phoneIcon.left-24.5, 15, 0.5, bottomLineForBuyer.top-30);
                buyerPhoneNumber.frame = CGRectMake(buyerName.right+5.0, buyerName.top, phoneLine.left-buyerName.right-23, buyerName.height);
                //加载
                [self addStorage:buyerTitle];
                [self addStorage:buyerIcon];
                [self addStorage:buyerName];
                [self addStorage:buyerPhoneNumber];
                [self addStorage:phoneLine];
                [self addStorage:phoneIcon];
                [self addStorage:bottomLineForBuyer];
            }
            
            //发布时间标题
            LWTextStorage *publishedTimeTitle = [self setupStorageWithFontSize:13 textColor:@"#666666"];
            publishedTimeTitle.frame = CGRectMake(18, bottomLineForBuyer.bottom+18, 120, CGFLOAT_MAX);
            publishedTimeTitle.text = @"发布时间";
            [self addStorage:publishedTimeTitle];
            //发布时间icon
            LWImageStorage *publishedTimeIcon = [[LWImageStorage alloc] init];
            publishedTimeIcon.contents = [UIImage imageNamed:@"发布时间"];
            publishedTimeIcon.frame = CGRectMake(publishedTimeTitle.left, publishedTimeTitle.bottom+12, 15, 15);
            [self addStorage:publishedTimeIcon];
            //发布时间内容
            LWTextStorage *publishedTimeContent = [self setupStorageWithFontSize:14*Radio textColor:@"#333333"];
            publishedTimeContent.frame = CGRectMake(publishedTimeIcon.right+8, publishedTimeIcon.top, Screen_Width/2.0-41, publishedTimeIcon.height);
            publishedTimeContent.text = model.create_time;
            [self addStorage:publishedTimeContent];
          
            //截止时间
            LWTextStorage *deadTimeTitle = [self setupStorageWithFontSize:13 textColor:@"#666666"];
            deadTimeTitle.text = @"截止时间";
            deadTimeTitle.frame = CGRectMake(Screen_Width/2.0+9.0, publishedTimeTitle.top, 120, CGFLOAT_MAX);
            [self addStorage:deadTimeTitle];
            
            //截止时间icon
            LWImageStorage *deadTimeIcon = [[LWImageStorage alloc] init];
            deadTimeIcon.contents = [UIImage imageNamed:@"截止时间"];
            deadTimeIcon.frame = CGRectMake(deadTimeTitle.left, deadTimeTitle.bottom+12, 15, 15);
            [self addStorage:deadTimeIcon];
            
            //截止时间内容
            LWTextStorage *deadTimeContent = [self setupStorageWithFontSize:14*Radio textColor:@"#333333"];
            deadTimeContent.frame = CGRectMake(deadTimeIcon.right+8, deadTimeIcon.top, Screen_Width/2.0-41, publishedTimeIcon.height);
            deadTimeContent.text = model.deadline_time;
            [self addStorage:deadTimeContent];
            //
            NSArray *titleList = @[@"任务类型",@"字数",@"语种"];
            NSDictionary *imageDictionary = @{
                                              @"1":@"翻译",
                                              @"2":@"编辑",
                                              @"3":@"校对"};
            NSArray *contentList = @[imageDictionary[model.tasktype],
                                     [NSString stringWithFormat:@"%.0f字",model.wordcount],
                                     [NSString stringWithFormat:@"%@ - %@",model.sourcelangname,model.targetlangname]];
            for (NSInteger index = 0; index < 3; ++ index) {
                
                LWImageStorage *line = [[LWImageStorage alloc] init];
                line.backgroundColor = [UIColor colorWithHexString:@"#e0e0e0"];
                line.frame = CGRectMake(18, publishedTimeContent.bottom+12+index*44.0, Screen_Width-36, 0.5);
                [self addStorage:line];
                
                LWTextStorage *title = [self setupStorageWithFontSize:14 textColor:@"#666666"];
                title.text = titleList[index];
                title.frame = CGRectMake(18, line.bottom+15, 100, CGFLOAT_MAX);
                [self addStorage:title];
                
                LWTextStorage *content = [self setupStorageWithFontSize:14 textColor:@"#333333"];
                content.frame = CGRectMake(title.right+10, title.top, Screen_Width-title.right-28, CGFLOAT_MAX);
                content.text = contentList[index];
                content.textAlignment = NSTextAlignmentRight;
                [self addStorage:content];
            }
            if (type == MJTaskOrderTranslateExample) {//试译中(添加提示)
            
                LWTextStorage *remark = [self setupStorageWithFontSize:13*Radio textColor:@"#ff6f4c"];
                remark.text = @"提示：";
            
                remark.frame = CGRectMake(18, publishedTimeContent.bottom+30+3*44.0, 50, CGFLOAT_MAX);
                [self addStorage:remark];
                
                LWTextStorage *text_One = [self setupStorageWithFontSize:13*Radio textColor:@"#666666"];
                text_One.frame = CGRectMake(remark.right, remark.top, Screen_Width-18-remark.right, CGFLOAT_MAX);
                text_One.text = @"此任务需先试译200字，请到Web端完成试译";
                [self addStorage:text_One];
                
                LWTextStorage *text_Two = [self setupStorageWithFontSize:13*Radio textColor:@"#666666"];
                text_Two.frame = CGRectMake(text_One.left, text_One.bottom+8, text_One.width, CGFLOAT_MAX);
                text_Two.text = [NSString stringWithFormat: @"试译截止时间：%@",model.deadline_time];
                [self addStorage:text_Two];

            }
        
        }else{//标签，任务说明
            NSString *typeStr = model.label_idname_list;
//            typeStr = @"标签,任务说明,水电气,写代码是真的难受,哈哈,标签,任务说明,水电气,写代码是真的难受,哈哈";
            NSArray *types = [typeStr componentsSeparatedByString:@","];
            NSMutableArray *tagImageList = [NSMutableArray array];
            if (types.count > 0) {
                //行
                NSInteger nextLineX = 0;
                //列
                NSInteger row = 0;
                
                for (NSString *type in types) {
                    
                    NSArray *arr = [type componentsSeparatedByString:@"_"];
                    LWImageStorage *tagImage = [[LWImageStorage alloc] init];
                    CGFloat titleWidth = [self calulateTextHeightWithString:arr.lastObject fontSize:13];
                    tagImage.contents = [self tagLabelWithTitle:arr.lastObject];
                    tagImage.contentMode = UIViewContentModeCenter;
                    [self addStorage:tagImage];
                    if (titleWidth+nextLineX+18 > Screen_Width) {
                        row += 1;
                        nextLineX = 0;
                    }
                    tagImage.frame = CGRectMake(18+nextLineX, 15+row*30, titleWidth, 20);
                    nextLineX += titleWidth+10;

                    [tagImageList addObject:tagImage];
                }
            }

            LWImageStorage *lastImageTag = [tagImageList lastObject];
            LWTextStorage *taskDescription = [self setupStorageWithFontSize:14 textColor:@"#000000"];
            taskDescription.text = @"任务说明";
            taskDescription.frame = CGRectMake(18, lastImageTag.bottom+15, 150, CGFLOAT_MAX);
            [self addStorage:taskDescription];
            
            
            LWTextStorage *decriptinContent = [self setupStorageWithFontSize:13 textColor:@"#333333"];
            decriptinContent.frame = CGRectMake(taskDescription.left, taskDescription.bottom+9, Screen_Width-36, CGFLOAT_MAX);
            decriptinContent.text = model.memo;
            [self addStorage:decriptinContent];
        }
        self.cellHeight = [self suggestHeightWithBottomMargin:28];
    }
    return self;
}
//初始化textStorage
- (LWTextStorage *)setupStorageWithFontSize:(NSInteger)fontSize textColor:(NSString *)textColor {
    LWTextStorage *textStorage = [[LWTextStorage alloc] init];
    textStorage.font = [UIFont systemFontOfSize:fontSize];
    textStorage.textColor = [UIColor colorWithHexString:textColor];
    return textStorage;
}
//计算文字长度
- (CGFloat)calulateTextHeightWithString:(NSString *)string fontSize:(CGFloat)fontSize {
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    CGRect rect = [string boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil];
    return rect.size.width+20;
}

//将label转化为UIImage对象
- (UIImage *)tagLabelWithTitle:(NSString *)title {
    UILabel *label = [[UILabel alloc] init];
    CGFloat titleWidth = [self calulateTextHeightWithString:title fontSize:13];
    label.layer.masksToBounds = YES;
    label.layer.cornerRadius = 10;
    label.textColor = mainColor;
    label.layer.borderColor = mainColor.CGColor;
    label.layer.borderWidth = 0.5;
    label.font = [UIFont systemFontOfSize:13];
    label.textAlignment = NSTextAlignmentCenter;
    label.frame = CGRectMake(18, 12, titleWidth, 20);
    label.text = title;
    UIGraphicsBeginImageContextWithOptions(label.bounds.size, NO, [UIScreen mainScreen].scale);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [label.layer renderInContext:ctx];
    UIImage* tImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return tImage;
}
@end
